﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using Sector7.Framework.Utility;
using Sector7.Medical.Consts;
using Sector7.Medical.EntityType;
using Sector7.Framework.EntityType;
using Sector7.Framework.Consts;

namespace Sector7.Medical.Services
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“TransferService”。
    public class TransferService : ITransferService
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public TransferResult UploadFile(TransferParameter param)
        {
            var result = new TransferResult();
            result.ErrorCode = C_ErrorCode.ERR_SYS_SUCCESS;
            result.ErrorMessage = string.Empty;
            result.SourceStream = new MemoryStream();
            
            if (param == null)
            {
                result.ErrorCode = C_ErrorCode.ERR_SYS_INVALID_PARAM;
            }
            else if (string.IsNullOrEmpty(param.FileName))
            {
                result.ErrorCode = C_ErrorCode.ERR_SYS_NULL_STRING;
            }
            else if (string.IsNullOrEmpty(param.DirectoryName))
            {
                result.ErrorCode = C_ErrorCode.ERR_SYS_NULL_STRING;
            }
            else if (param.SourceStream == null)
            {
                result.ErrorCode = C_ErrorCode.ERR_SYS_NULL_OBJECT;
            }
            else
            {
                var sourceStream = param.SourceStream;

                var targetPath = PathUtil.GenerateAbsPath(param.DirectoryName);
                PathUtil.CheckExists(targetPath);
                targetPath = PathUtil.CombinePath(targetPath, param.FileName);
                var targetStream = new FileStream(targetPath, FileMode.Create, FileAccess.Write);

                try
                {
                    var buffer = new byte[C_MaxSize.STREAM_BUFFER_MAX_SIZE];
                    int count = 0;
                    do
                    {
                        count = sourceStream.Read(buffer, 0, C_MaxSize.STREAM_BUFFER_MAX_SIZE);
                        targetStream.Write(buffer, 0, count);
                    } while (count > 0);
                }
                finally
                {
                    GCUtil.CloseStream(sourceStream);
                    GCUtil.CloseStream(targetStream);
                }
            }

            return result;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public TransferResult DownloadFile(TransferParameter param)
        {
            var result = new TransferResult();
            result.ErrorCode = C_ErrorCode.ERR_SYS_SUCCESS;
            result.ErrorMessage = string.Empty;
            result.SourceStream = null;

            if (param == null)
            {
                result.ErrorCode = C_ErrorCode.ERR_SYS_INVALID_PARAM;
            }
            else if (string.IsNullOrEmpty(param.FileName))
            {
                result.ErrorCode = C_ErrorCode.ERR_SYS_NULL_STRING;
            }
            else if (string.IsNullOrEmpty(param.DirectoryName))
            {
                result.ErrorCode = C_ErrorCode.ERR_SYS_NULL_STRING;
            }
            else
            {
                string fileName = PathUtil.GenerateAbsPath(param.DirectoryName, param.FileName);
                if(File.Exists(fileName))
                {
                    result.SourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                }
                else
                {
                    result.ErrorCode = C_ErrorCode.ERR_SYS_FILE_NOT_FOUND;
                }
            }

            return result;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="param">文件路径</param>
        /// <returns></returns>
        public TransferResult DeleteFile(TransferParameter param)
        {
            var result = new TransferResult();
            result.ErrorCode = 0;
            result.ErrorMessage = string.Empty;

            if (param == null)
            {
                result.ErrorCode = -1;
            }
            else if (string.IsNullOrEmpty(param.FileName))
            {
                result.ErrorCode = -2;
            }
            else if (param.SourceStream == null)
            {
                result.ErrorCode = -3;
            }
            else
            {
                string fileName = PathUtil.Combine(
                    ApplicationUtil.GetBaseDirectory(), 
                    param.FileName);
                if (File.Exists(fileName))
                {
                }
                else
                {
                    result.ErrorCode = -4;
                }
            }

            return result;
        }
    }
}
